package main

import (
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/PuerkitoBio/goquery"
	"github.com/realint/dbgutil"
)

var p = fmt.Println
var pp = dbgutil.Display

type dateStruct map[string][]string

var date dateStruct
var dateLock sync.Mutex

//读取 id 生成器
func readId() chan int {
	out := make(chan int)
	f, _ := os.Open("id.txt")
	var n int
	go func() {
		defer func() {
			p("########## closed")
			f.Close()
		}()
		for {
			_, err := fmt.Fscanf(f, "%d\n", &n)
			p(n)
			out <- n
			if err != nil {
				close(out)
				break
			}
		}
	}()
	return out
}

// 保存
func save() {
	for {
		dateLock.Lock()
		j, _ := json.Marshal(date)
		dateLock.Unlock()
		f, _ := os.OpenFile("date/"+strconv.Itoa(int(time.Now().Unix()))+".json", os.O_CREATE|os.O_WRONLY, 0)
		f.WriteString(string(j))
		p("saved")
		f.Close()
		time.Sleep(time.Second * 10)
	}
}

func main() {
	date = make(dateStruct)
	idService := readId()
	go save()
	ch := make(chan int, 10)
	for {
		time.Sleep(time.Millisecond * 1)
		id, ok := <-idService
		if ok != true {
			break
		}
		//p("read", id)
		get(id, ch)
		//p("over", id)
	}
	save()
	//for {
	//	if len(ch) == 0 {
	//		save()
	//		p("ok all")
	//		break
	//	}
	//}
}

func get(n int, ch chan int) {
	p("get", n, len(ch))
	ch <- 1
	defer func() {
		<-ch
	}()

	var doc *goquery.Document
	var e error
	if doc, e = goquery.NewDocument("http://211.67.208.69/kdjw/xscjcx.jsp?yzbh=" + strconv.Itoa(n)); e != nil {
		if e != nil {
			return
		}
	}
	doc.Find("#xscjxx tr").Each(func(i int, tr *goquery.Selection) {
		if i < 2 {
			return
		}
		name := tr.Find(":nth-child(1)").Text()
		grade := tr.Find(":nth-child(3)").Text()
		if grade == "" {
			return
		}
		name = strings.Replace(name, "▲", "", 2)
		name = strings.Replace(name, string([]byte{194, 160}), "", 2)
		//fmt.Println(name, "#", grade)
		dateLock.Lock()
		date[name] = append(date[name], grade)
		dateLock.Unlock()
	})
	recover()

}
